package com.poker.ui;

import com.landlords.model.Poker;
import com.poker.constants.Constants;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

/**
 * 自定义的扑克的图片
 * 
 * @author samoin
 * @since 2011-06-28
 * 
 */
public class MyPokerImageView extends ImageView {

	boolean isAnimation = false;

	private Poker poker;

	private boolean isClick = false;// 当前牌是否选中状态
	private int curX = 0;// 当前的X偏移量
	private int curY = 0;// 当前的Y偏移量

	public int getCurX() {
		return curX;
	}

	public Poker getPoker() {
		return poker;
	}

	public void setPoker(Poker poker) {
		this.poker = poker;
	}

	public void setCurX(int curX) {
		this.curX = curX;
	}

	public int getCurY() {
		return curY;
	}

	public void setCurY(int curY) {
		this.curY = curY;
	}

	public boolean isClick() {
		return isClick;
	}

	public void setClick(boolean isClick) {
		this.isClick = isClick;
	}

	public MyPokerImageView(Context context) {
		super(context);
		this.setOnClickListener(new PokerImageViewOnClickListener());
	}

	public MyPokerImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyPokerImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	// 重置相关属性为默认空
	public void resetEmptyPros() {
		this.isClick = false;
		this.curX = 0;
		this.curY = 0;
		this.poker = null;
		this.setBackgroundDrawable(null);
		this.setImageBitmap(null);
	}

	// 从父级中移除
	public void removeFromParent() {
		ViewGroup vg = (ViewGroup) this.getParent();
		if (vg != null) {
			vg.removeViewInLayout(this);
		}
		this.resetEmptyPros();
	}

	/**
	 * 扑克点击的事件类
	 * 
	 * @author samoin
	 * 
	 */
	class PokerImageViewOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			MyPokerImageView view = (MyPokerImageView) v;
			if (!view.isClick()) {
				if (!isAnimation) {
					RelativeLayout.LayoutParams lp = (LayoutParams) v
							.getLayoutParams();
					RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.WRAP_CONTENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
					lp2.setMargins(lp.leftMargin, lp.topMargin
							- Constants.POKER_UP_DISTANCE, lp.rightMargin,
							lp.bottomMargin);
					view.setLayoutParams(lp2);
				} else {
					int endY = view.getCurY() - Constants.POKER_UP_DISTANCE;
					TranslateAnimation animation = new TranslateAnimation(0, 0,
							view.getCurY(), endY);
					animation.setDuration(Constants.GOCARDS_ANIMATION_MILESEC);
					animation.setFillAfter(true);
					view.startAnimation(animation);
					view.setCurY(endY);
				}
				view.setClick(true);
			} else {
				if (!isAnimation) {
					RelativeLayout.LayoutParams lp = (LayoutParams) v
							.getLayoutParams();
					RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.WRAP_CONTENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
					lp2.setMargins(lp.leftMargin, lp.topMargin
							+ Constants.POKER_UP_DISTANCE, lp.rightMargin,
							lp.bottomMargin);
					view.setLayoutParams(lp2);
				} else {
					int endY = view.getCurY() + Constants.POKER_UP_DISTANCE;
					TranslateAnimation animation = new TranslateAnimation(0, 0,
							view.getCurY(), endY);
					animation.setDuration(Constants.GOCARDS_ANIMATION_MILESEC);
					animation.setFillAfter(true);
					view.startAnimation(animation);
					view.setCurY(endY);
				}
				view.setClick(false);
			}
		}

	}
}
